<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>测试功能的页面</title>
    <link rel="icon" type="image/png" href="../img/icon.png">
    <link rel="stylesheet" href="../css/all.css">
</head>
<body>
<div>
    <h2>批量录入题目</h2>
    <label>
        文件名:
        <input id="fileNameInput" type="text" style="width: 300px;" placeholder="请输入服务器内对应的文件名">
    </label>
    <br>
    <button onclick="doInitQuestion()" >开始导入数据</button>
</div>
<hr>
<div>
    <h2>创建考试</h2>
    <label>
        考试名称:
        <input id="examNameInput" type="text" style="width: 300px;" placeholder="请输入考试名称">
    </label>
    <br>
    <button onclick="doCreateExam()" >创建</button>
</div>
<hr>
<div>
    <h2>考试</h2>
    <label>
        考试编号:
        <input id="examPaperIdInput" type="text" style="width: 300px;" placeholder="请输入考试编号">
    </label>
    <br>
    <button onclick="doStartExam()" >开始考试</button>
    <button onclick="doFinishExam()" >结束考试, 自动批改选择题</button>
</div>
<hr>
<div>
    <h2>重置密码为 1</h2>
    <label>
        用户编号:
        <input id="userIdInput" type="text" style="width: 300px;" placeholder="请输入用户编号">
    </label>
    <label>
        学生编号:
        <input id="stuIdInput" type="text" style="width: 300px;" placeholder="请输入学生编号">
    </label>
    <br>
    <button onclick="doResetPwd()" >重置</button>
</div>
<hr>
<div>
    <h2>批改某个学生的试卷</h2>
    <label>
        试卷编号:
        <input id="examPaperIdInput2" type="text" style="width: 300px;" placeholder="请输入试卷编号">
    </label>
    <label>
        学生编号:
        <input id="stuIdInput2" type="text" style="width: 300px;" placeholder="请输入学生编号">
    </label>
    <br>
    <button onclick="doGotoDetailPage()" >进入批改界面</button>
</div>
<hr>
<div>
    <h2>进入试卷管理界面</h2>
    <button onclick="doGotoExamManagePage()" >试卷管理</button>
</div>


<script src="../js/vue.min.js" ></script>
<script src="../js/jquery-3.3.1.min.js" ></script>
<script src="../js/toastr.min.js" ></script>
<script src="../js/base.js" ></script>
<script>
    // 模拟题目的批量录入 ok
    function doInitQuestion() {
        let fileName = $("#fileNameInput").val();
        $.getJSON(baseUrl + '/json/' + fileName, (result) => {
            let arr = result;

            console.log(arr);
            let {typeId, type, language, score} = arr[0];

            // doAddQt(arr[0]);

            let t = setInterval(()=>{
                let last = arr.pop();
                // let last = arr[arr.length - 1];
                Object.assign(last, {typeId, type, language, score});
                //console.log(last);
                doAddQt(last);
                if(arr.length <= 0){
                    clearInterval(t);
                }
            }, 500);
        });
    }

    // 录入一个题目 ok
    function doAddQt(qt) {
        qt.optionList = JSON.stringify(qt.optionList);
        $.ajax({
            type: 'post',
            url: baseUrl + "/back/qt/add",
            data: qt,
            dataType: 'json',
            success: (result) => {
                console.log('ok', result);
            },
            error: (xhr) => {
                console.log('异常:', qt);
            }
        });
    }

    // 创建考试
    function doCreateExam() {
        let name = $("#examNameInput").val();
        if(!name || !name.trim()) {
            return;
        }
        $("#examNameInput").val('');
        $.ajax({
            type: 'post',
            url: baseUrl + "/back/paper/createExam",
            data: {name},
            dataType: 'json',
            success: (result) => {
                alert(result.msg);
            },
            error: (xhr) => {
                alert('异常');
            }
        });
    }
    
    // 开始考试, 修改试卷状态
    function doStartExam() {
        let examPaperId = $("#examPaperIdInput").val();
        if(!examPaperId || !examPaperId.trim()) {
            return;
        }
        $.ajax({
            type: 'post',
            url: baseUrl + "/back/paper/startExam",
            data: {examPaperId},
            dataType: 'json',
            success: (result) => {
                alert(result.msg);
            },
            error: (xhr) => {
                alert('异常');
            }
        });
    }
    
    // 结束考试, 会自动批改选择题 ok
    function doFinishExam() {
        let examPaperId = $("#examPaperIdInput").val();
        if(!examPaperId || !examPaperId.trim()) {
            return;
        }
        $.ajax({
            type: 'post',
            url: baseUrl + "/back/paper/finishExam",
            data: {examPaperId},
            dataType: 'json',
            success: (result) => {
                alert(result.msg);
            },
            error: (xhr) => {
                alert('异常');
            }
        });
    }

    // 密码重置
    function doResetPwd() {
        let userId = $("#userIdInput").val();
        let stuId = $("#stuIdInput").val();

        $("#userIdInput").val('');
        $("#stuIdInput").val('');
        $.ajax({
            type: 'post',
            url: baseUrl + "/back/user/resetPwd",
            data: {userId, stuId},
            dataType: 'json',
            success: (result) => {
                alert(result.msg);
            },
            error: (xhr) => {
                alert('异常');
            }
        });
    }

    function doGotoDetailPage() {
        let examPaperId = $("#examPaperIdInput2").val();
        let stuId = $("#stuIdInput2").val();

        // "/back/paper/previewExam"
        location.href = baseUrl + "/manage/stu_exam_paper_marking.html?examPaperId="+examPaperId+"&stuId="+stuId;
    }

    function doGotoExamManagePage() {
        location.href = baseUrl + "/manage/examManage.html";
    }


    let root = document.documentElement;
    if(!root) {
        root = document.getElementsByTagName("html")[0];
    }
    root.fullscreenEnabled = true;
    root.onfullscreenchange = function () {
        let fullEle = document.fullscreenElement;
        console.log('fullscreenElement:', fullEle);
    };

    function d1() {
        root.requestFullscreen().catch((err)=>{
            console.log('requestFullscreen-err:', err);
        });
    }


    fullObj.init(function () {
        if(!fullObj.isFull()) {
            console.log('您已退出全屏, 已违规！');
        }
    });
    document.onkeypress = function (e) {
        if(e.key == "1") {
            if(!fullObj.isFull()) {
                fullObj.doFullScreen();
            } else {
                fullObj.doExitScreen();
            }
        }
    }
</script>
</body>
</html>